<?php

namespace app\api\model\Collage;



use think\facade\Db;
use think\Model;

class CollageModel extends Model
{

    /**
     * 数据表名
     * @var string
     */
    protected $name = "collage";

    /**
     * 根据条件修改
     * @param array $where 条件
     * @param array $save 修改的数据
     * @return int|string
     * @throws
     */
    public static function updateCollageData($where,$save)
    {
        $flag = Db::name("collage")->where($where)->update($save);
        return $flag;
    }

    /**
     * 分页查询
     * @param $page
     * @param $limit
     * @param array $where
     * @param string $field
     * @return array
     * @throws
     */
    public static function getCollageListWherePage($page,$limit,$where = [],$field = "*")
    {
        $start = ceil(($page-1)*$limit);
        $list = Db::name('collage')->field($field)->where($where)->limit($start,$limit)->select()->toArray();
        return $list;
    }

    /**
     * 统计
     * @param $where
     * @return int|string
     * @throws
     */
    public static function getCollageListWhereCount($where)
    {
        $count = Db::name('collage')->where($where)->count("id");
        return $count;
    }

    /**
     * 查询更多
     * @param $where
     * @param string $field
     * @return array
     * @throws
     */
    public static function getCollageListWhereMore($where,$field="c.*")
    {
        $list = Db::name('collage')
            ->alias('c')
            ->join('user u','c.uid = u.id','left')
            ->field($field)
            ->where($where)
            ->select()->toArray();
        return $list;
    }

    /**
     * 添加拼团
     * @param $data
     * @return int|string
     */
    public static function addCollage($data)
    {
        $id = Db::name('collage')->insertGetId($data);
        return $id;
    }

    /**
     * 根据条件查询单条
     * @param $where
     * @param string $field
     * @return array|Model|null
     * @throws
     */
    public static function getCollageWhere($where,$field = "*")
    {
        $collage = Db::name('collage')->field($field)->where($where)->find();
        return $collage;
    }

}